package com.changpay.zywy.ui.activity.freeze

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.FreezeListAdapter
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.*
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kotlinx.android.synthetic.main.fragment_freeze_two.*


class FreezeTwoFragment : BaseFragment(), IView {

    var member_id: String = ""
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }


    companion object {
        fun getInstance(): FreezeTwoFragment {
            val bundle = Bundle()
            val fragment = FreezeTwoFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_freeze_two

    override fun lazyLoad() {

    }

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.tv_project_name, R.id.tv_daili_name ->
                Intent(context, FreezePeopleActivity::class.java).run {
                    putExtra("type",2)
                    startActivityForResult(this, 100)

                }
        }
    }

    override fun initView(view: View) {

        initRecyclerView()
        tv_daili_name.setOnClickListener(onClickListener)
        tv_daili_phone.setOnClickListener(onClickListener)
    }

    /**
     * datas
     */
    private val datas_one = mutableListOf<FreezeMyBean.FreezeListBean>()
    /**
     * Adapter
     */
    private val mAdapter_one: FreezeListAdapter by lazy {
        FreezeListAdapter(context, datas_one)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    private fun loadData() {

        val param = hashMapOf<String, String>()
        param["key"] = token
        param["member_id"] = member_id
        RetrofitHelper.service.get_freeze_list(param).sss(this)
        {
            datas_one.clear()
            datas_one.addAll(it.result.freeze_list)
            mAdapter_one.notifyDataSetChanged()
        }
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        val data = datas_one[position]
        Intent(activity, FreezeDetailActivity::class.java).run {
            putExtra("id", data.id.toString())
            putExtra("title", "冻结详情")
            startActivity(this)
        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter_one
        }

        mAdapter_one.run {
            bindToRecyclerView(recyclerView)
            onItemClickListener = this@FreezeTwoFragment.onItemClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            tv_daili_name.text = data?.getStringExtra("member_name")
            tv_daili_phone.text = data?.getStringExtra("member_phone")
            member_id = data?.getStringExtra("member_id")!!
            loadData()
        }
    }

    override fun onResume() {
        super.onResume()
        if (member_id.isEmpty()) return
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["member_id"] = member_id
        RetrofitHelper.service.get_freeze_list(param).sss(this)
        {
            datas_one.clear()
            datas_one.addAll(it.result.freeze_list)
            mAdapter_one.notifyDataSetChanged()
        }
    }
}






